﻿using System;
using System.Globalization;
using GeoFramework;
using Microsoft.WindowsMobile.Samples.Location;

namespace Lapd.Common.Helpers
{
    public class GpsHelper : IGpsHelper
    {
        private Position _homePosition;

        public GpsHelper()
        {
            double homeLatitudeDouble = Double.Parse(MobileConfiguration.Settings["HomeLatitude"], CultureInfo.InvariantCulture);
            double homeLongitudeDouble = Double.Parse(MobileConfiguration.Settings["HomeLongitude"], CultureInfo.InvariantCulture);

            Latitude homeLatitude = new Latitude(homeLatitudeDouble);
            Longitude homeLongitude = new Longitude(homeLongitudeDouble);
            _homePosition = new Position(homeLatitude, homeLongitude);
        }

        public bool OutOfPerimeter(GpsPosition currentGpsPosition)
        {
            // Convert GpsPosition from namespace Microsoft.WindowsMobile.Samples.Location 
            // to Position from GeoFramework
            Position currentPosition = new Position(
                currentGpsPosition.Latitude.ToString(), 
                currentGpsPosition.Longitude.ToString());

            // Get TopDistance value from App.config
            double topDistance = Double.Parse(MobileConfiguration.Settings["TopDistance"]);

            return _homePosition.DistanceTo(currentPosition).Value > topDistance;
        }
       
    }
}
